package cn.sundat.service;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;

import cn.sundat.dao.CompanyProductDao;
import cn.sundat.dao.ProductDao;
import cn.sundat.model.Company;
import cn.sundat.model.CompanyProduct;
import cn.sundat.model.PageBean;
import cn.sundat.model.Product;

@Transactional
public class CompanyProductServiceImpl implements CompanyProductService {
	@Resource(name="companyProductDao")
	private CompanyProductDao companyProductDao;
	
	@Resource(name="productDao")
	private ProductDao productDao;
	
	@Override
	public void save(CompanyProduct companyProduct) {
		companyProductDao.save(companyProduct);
	}
	

	@Override
	public PageBean<CompanyProduct> findPBByCName(Integer currPage, Integer pageSize, Company company) {
		DetachedCriteria criteria = DetachedCriteria.forClass(CompanyProduct.class);
		criteria.add(Restrictions.eq("company", company));
		return companyProductDao.findByPage(currPage, pageSize, criteria);
	}


	@Override
	public CompanyProduct getById(String cp_id) {
		return companyProductDao.getById(cp_id);
	}


	@Override
	public PageBean<CompanyProduct> findByPId(Integer currPage, Integer pageSize, String pro_id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(CompanyProduct.class);
		criteria.add(Restrictions.eq("product.pro_id", pro_id));
		PageBean<CompanyProduct> list = companyProductDao.findByPage(currPage, pageSize, criteria);
		return list;
	}


	@Override
	public Product findIdByName(String pro_name) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Product.class);
		criteria.add(Restrictions.like("pro_name", "%"+pro_name+"%"));
		Product p = productDao.findByName(criteria);
		return p;
	}


	@Override
	public void delete(CompanyProduct cp) {
		//删除文件
		
		//删除数据库中数据
		companyProductDao.delete(cp);
	}


	@Override
	public List<CompanyProduct> findPType(String cate_id) {
		return companyProductDao.findPType(cate_id);
	}


	@Override
	public void update(CompanyProduct cp) {
		companyProductDao.update(cp);
	}
	
}
